using UnityEngine;
using System.Collections;

public class MainCamera : MonoBehaviour {
	
	public Transform player = null;
	Vector3 worldPos;
	float mouseX;
	float mouseY;
	float cameraDif;
	public string tagPlayer;
	NetworkView netView;
	
	// Use this for initialization
	void Start () 
	{
		
	}
	
	// Update is called once per frame
	void Update () 
	{	
		while(player == null)
		{
			player = GameObject.FindGameObjectWithTag(tagPlayer).transform;
			cameraDif = camera.transform.position.y - player.transform.position.y;
			netView = player.GetComponent<NetworkView>();
		}
		
		if(netView.isMine)
		{
			Vector3 position = new Vector3(player.transform.position.x, transform.position.y, player.transform.position.z);
			
			mouseX = Input.mousePosition.x;
	    	mouseY = Input.mousePosition.y;
	
	    	worldPos = camera.ScreenToWorldPoint(new Vector3(mouseX, mouseY, cameraDif));
	
	    	Vector3 turretLookDirection = new Vector3(worldPos.x,player.transform.position.y, worldPos.z);
	
	    	player.transform.LookAt(turretLookDirection);
			
			transform.position = position;
			
			if(player.tag == "Player2")
			{
				camera.depth = 0;
			}
		}
	}
}
